package com.chen.runnabletask;

import com.chen.infa.ChatMessageListener;
import com.chen.infa.ChatUpdateUserListListener;
import com.chen.infa.PrivateChatMessageListener;
import com.chen.util.AppContext;
import com.chen.util.Constant;
import lombok.Setter;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Setter
public class ClientThread implements Runnable {
    private static final ClientThread thread = new ClientThread();
    private final Socket socket = AppContext.getInstance().getClientSocket();
    private DataInputStream in;
    private PrivateChatMessageListener privateChatMessageListener;
    private ChatMessageListener chatMessageListener;
    private ChatUpdateUserListListener chatUpdateUserListListener;

    private ClientThread() {
    }

    public static ClientThread getInstance() {
        return thread;
    }

    @Override
    public void run() {
        try {
            in = new DataInputStream(socket.getInputStream());
            while (true) {
                String type = in.readUTF();
                switch (type) {
                    case Constant.UPDATE_USER:
                        // 更新用户列表
                        updateOnLineUserList();
                        break;
                    case Constant.ALL_MSG:
                        // 每个客户端接收服务器群发的消息并展示到消息框
                        showMsgToAll();
                        break;
                    case Constant.PERSONAL_MSG:
                        // 实现针对私聊对象，将私聊消息展示给自己和私聊对象
                        showMsgToPersonal();
                        break;
                }
            }
        } catch (Exception e) {
            System.out.println("客户端接收消息错误");
            e.printStackTrace();
        }
    }

    private void showMsgToPersonal() {
        try {
            String msg = in.readUTF();
            String toUser = in.readUTF();
            String fromUser = in.readUTF();
            if (Objects.nonNull(privateChatMessageListener)) {
                privateChatMessageListener.onPrivateMessageReceived(fromUser, toUser, msg);
            }
        } catch (Exception e) {
            System.out.println("私聊客户端接收消息发生错误");
            e.printStackTrace();
        }
    }

    private void showMsgToAll() {
        try {
            // 获取转发消息
            String msg = in.readUTF();
            if (Objects.nonNull(chatMessageListener)) {
                chatMessageListener.onMessageReceived(msg);
            }
        } catch (IOException e) {
            System.out.println("客户端showMsgToAll()出错");
            e.printStackTrace();
        }
    }

    private void updateOnLineUserList() {
        try {
            // 创建集合收集在线用户名称
            List<String> onLineUsers = new ArrayList<>();
            // 获取集合大小
            int size = in.readInt();
            for (int i = 0; i < size; i++) {
                onLineUsers.add(in.readUTF());
            }

            // 调用聊天界面对象，将在线用户列表展示
            if (Objects.nonNull(chatUpdateUserListListener)) {
                chatUpdateUserListListener.updateUserList(onLineUsers);
            }
        } catch (IOException e) {
            System.out.println("客户端updateOnLineUserList()出错");
            e.printStackTrace();
        }
    }
}
